import re

import pandas as pd
from loguru import logger

from DB import mongo_client_cloud, MongoClient
from utils import create_md5

Mongo_HOST = '192.168.44.131'
Mongo_PORT = '27017'
Mongo_DB = 'ChineseJournals'
Mongo_USERNAME = 'ChineseJournals_admin'
Mongo_PASSWORD = 'ggcEnYfwj4ZfCAVI'
mongo_connect = {
    "host": Mongo_HOST,
    "user": Mongo_USERNAME,
    "pwd": Mongo_PASSWORD,
    'port': Mongo_PORT,
    "db": Mongo_DB,
}

mongo_client = MongoClient(mongo_connect)


def is_all_digits(text):
    if re.match("^\d+$", text):
        return True
    else:
        return False


def TO_Excel(collection, data=None):
    logger.debug(f"开始处理 {collection} 数据")
    res_data = [doc for doc in data]

    # 转换为DataFrame对象
    logger.debug(f"开始转换 {collection} 为DataFrame对象")
    df = pd.DataFrame(res_data)
    df_cleaned = df.replace({re.compile(r'[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]'): ''}, regex=True)
    df_cleaned.to_excel(f"D:\workSpace\\00_临时任务\data\\{collection}.xlsx", index=False)


if __name__ == '__main__':
    projection = {
        "_id": 0,
        "former_title": 0,
        "create_time": 0,
        "update_timestamp": 0,
        "update_time": 0,
        "uid": 0,
        "md5": 0,
        "admin_istrator": 0,
        "introduction_url":0,
        "officialwebsite_url":0,
        "postal_distribution_code":0,
        "governance":0,
        "compilation":0,
        "rate":0,
        "visits":0,
        "former_num":0,
        "unit_price":0,
        "Operating_unit":0,
        "Google_Quote":0,
        "impactfactor_fuhe":0,
        "Vip_Quote":0,
    }
    document_bool, document_data = mongo_client.find_document_new(f"JournalCleanTable2", {}, projection=projection, Type=2)
    res_list = []
    for _ in document_data:
        # source_journal_id = _.get("source_journal_id","")
        # if not source_journal_id:
        #     source = _["source"]
        #     journal_id = _["journal_id"]
        #     source_journal_id = {
        #         "source": source,
        #         "journal_id": journal_id
        #     }
        # _["source_journal_id"] = source_journal_id
        # source_journal_url = _.get("source_journal_url","")
        # if not source_journal_url:
        #     journal_url = _["journal_url"]
        #     source = _["source"]
        #     source_journal_url = {
        #         "source": source,
        #         "url": journal_url
        #     }
        #
        # _["source_journal_url"] = source_journal_url

        res_list.append({
            "CN": _.get("cn",""),
            "封面图片": _.get("coverUrl", ""),
            "编辑地址": _.get("editorialaddress",""),
            "主编": _.get("editorinchief",""),
            "EISSN": _.get("eissn",""),
            "期刊英文名称": _.get("en_journal_name",""),
            "成立年份": _.get("foundedYear",""),
            "ISSN": _.get("issn",""),
            "影响因子": _.get("impactfactor",""),
            "期刊简介": _.get("journal_introduce",""),
            "期刊名称": _.get("journal_name",""),
            "期刊官网": _.get("journal_url",""),
            "期刊语种": _.get("language",""),
            "出版周期": _.get("publication_period",""),
            "出版商": _.get("publisher",""),
            "承办商": _.get("purveyor",""),
            "刊物来源": _.get("source",""),
            "引用次数": _.get("CiteNum",""),
            "下载次数": _.get("DownloadNum",""),
            "发布数量": _.get("PublishNum",""),
            "被收录": _.get("database",""),
            "邮箱": _.get("email",""),
            "inlandembody": _.get("inlandembody",""),
            "期刊类型": _.get("journal_type",""),
            "listLngrangeid": _.get("listLngrangeid",""),
            "机构": _.get("organs",""),
            "电话": _.get("phone",""),
            "发布地址": _.get("place_of_publication",""),
            "邮政编码": _.get("postcode",""),
            "价格": _.get("price",""),
            "来源期刊ID": _.get("source_journal_id",""),
            "来源期刊官网": _.get("source_journal_url",""),
            "知网_OA": _.get("知网_OA",""),
            "万方维普_OA": _.get("万方维普_OA",""),
            "超星_OA": _.get("超星_OA",""),
            "一级分类": _.get("primary_classification_name",""),
            "二级分类": _.get("secondary_classification_name",""),
            "三级分类": _.get("three_level_classification_name",""),
        })


    # res_list = []
    # for _ in document_data:
    #     res_list.append(
    #         {
    #             "journal_name": _["journal_name"],
    #             "journal_id": _["journal_id"]
    #         }
    #     )
    TO_Excel("中文期刊刊物汇总表", res_list)
    # res_list = []
    # for _ in document_data:
    #     zhu_bian = _["zhu_bian"] if not is_all_digits(_["zhu_bian"]) else ""
    #     if "," in zhu_bian:
    #         zhu_bian = ";".join(zhu_bian.split(","))
    #     elif "，" in zhu_bian:
    #         zhu_bian = ";".join(zhu_bian.split("，"))
    #     elif " " in zhu_bian:
    #         zhu_bian = ";".join(zhu_bian.split(" "))
    #     elif "；" in zhu_bian:
    #         zhu_bian = ";".join(zhu_bian.split("；"))
    #     elif ";" in zhu_bian:
    #         zhu_bian = _["zhu_bian"]
    #     else:
    #         zhu_bian = _["zhu_bian"]
    #     # zhu_bian = ";".join(zhu_bian.split(",")) if "," in zhu_bian else ";".join(zhu_bian.split(" "))
    #     data = {
    #         "期刊ID": _["journal_id"],
    #         "期刊名称": _["journal_name"],
    #         "期刊链接": _["journal_url"],
    #         "期刊封面": _["coverUrl"],
    #         "cn": _["cn"],
    #         "issn": _["issn"],
    #         "期刊介绍": _["journal_introduce"],
    #         "出版周期": _["publication_period"],
    #         "影响因子": _["impactfactor"],
    #         "引用": _["CiteNum"],
    #         "下载量": _["DownloadNum"],
    #         "文献量": _["PublishNum"],
    #         "主类": _["primary_classification_name"],
    #         "次类": _["secondary_classification_name"],
    #         "主管单位": _["zhu_guan_dan_wei"],
    #         "主办单位": _["zhu_ban_dan_wei"],
    #         "主编": zhu_bian if not is_all_digits(zhu_bian) else "",
    #         "地址": _["di_zhi"],
    #         "邮政编码": _["you_zheng_bian_ma"],
    #         "邮箱": _["email"],
    #         "电话": _["dian_hua"],
    #     }
    #     res_list.append(data)
    # TO_Excel("wanfang_journals_info", res_list)